package com.tanxi521.springboot.controller;

import com.tanxi521.springboot.bean.NewRewardBean;
import com.tanxi521.springboot.bean.RewardBean;
import com.tanxi521.springboot.bean.RewardCardBean;
import com.tanxi521.springboot.pojo.*;
import com.tanxi521.springboot.service.RewardsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("rewards")
@Slf4j
@CrossOrigin
public class RewardsController {

    @Resource
    private RewardsService rewardsService;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("list")
    @RestPoint
    public APIResult getList(Integer pageSize, Integer pageIndex,
                             String rewardName, String rewardTag,
                             String rewardVipTag, Byte rewardIsOn) {
        Rewards rewards = new Rewards();
        rewards.setRewardName(rewardName);
        rewards.setRewardTag(rewardTag);
        rewards.setRewardVipTag(rewardVipTag);
        rewards.setRewardIsOn(rewardIsOn);
        List<Rewards> rewardsList = rewardsService.queryListByRewards(pageIndex, pageSize, rewards);
        int count = rewardsService.queryRewardsCountByRewards(rewards);
        for (Rewards rewards1 : rewardsList) {
            String rewardTag1 = rewards1.getRewardTag();
            if (rewardTag1.equals("001")) {
                rewards1.setRewardTag("无限制");
            } else if (rewardTag1.equals("002")) {
                rewards1.setRewardTag("每日礼包");
            } else if (rewardTag1.equals("003")) {
                rewards1.setRewardTag("每周礼包");
            }
            String rewardVipTag1 = rewards1.getRewardVipTag();
            if (rewardVipTag1.equals("001")) {
                rewards1.setRewardVipTag("无限制");
            } else if (rewardVipTag1.equals("002")) {
                rewards1.setRewardVipTag("VIP1");
            } else if (rewardVipTag1.equals("003")) {
                rewards1.setRewardVipTag("VIP2");
            } else if (rewardVipTag1.equals("004")) {
                rewards1.setRewardVipTag("VIP3");
            } else if (rewardVipTag1.equals("005")) {
                rewards1.setRewardVipTag("VIP4");
            } else if (rewardVipTag1.equals("006")) {
                rewards1.setRewardVipTag("VIP5");
            }
        }
        RewardBean rewardBean = new RewardBean();
        rewardBean.setRewardsList(rewardsList);
        rewardBean.setCount(count);
        rewardBean.setPageCount(count / pageSize + 1);
        return new APIResult(rewardBean, APIResult.SUCCESS, "查询奖励物品成功");
    }

    @PostMapping("rewardState")
    @RestPoint
    public APIResult changeState(@RequestBody Rewards rewards, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        Rewards rewards1 = rewardsService.selectByPrimaryKey(rewards);
        rewards1.setRewardIsOn(rewards.getRewardIsOn());
        int count = rewardsService.updateRewardStateById(rewards1);
        if (count != 0) {
            String isOn = rewards.getRewardIsOn() == 1 ? "启用" : "禁用";
            log.warn(oa_user.getUserName() + " 设置奖励 " + rewards.getRewardName() + " 为 " + isOn);
            return new APIResult(APIResult.SUCCESS, "设置 " + rewards.getRewardName() + " " + isOn  +"成功");
        } else {
            return new APIResult(APIResult.FAIL, "操作失败，请重试");
        }
    }

    @PostMapping("newReward")
    @RestPoint
    public APIResult newReward(@RequestBody NewRewardBean rewardBean, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = rewardsService.addNewRewards(rewardBean, oa_user.getUserName());

        if (count != 1) {
            try {
                throw new RuntimeException();
            } catch (RuntimeException e) {
                return new APIResult(APIResult.FAIL, "添加奖励礼包失败，请重试");
            }
        }
        return new APIResult(APIResult.SUCCESS, "添加成功！");
    }

    @GetMapping("userRewards")
    @RestPoint
    public APIResult userRewards(String test, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        String userName = oa_user.getUserName();
        List<RewardCard> rewardCards = rewardsService.queryUserRewards(userName);
        RewardCardBean rewardCardBean = new RewardCardBean();
        rewardCardBean.setRewardCards(rewardCards);
        return new APIResult(rewardCardBean, APIResult.SUCCESS, "获取奖励列表成功");
    }

    @PostMapping("getRewards")
    @RestPoint
    public APIResult getRewards(@RequestBody RewardCard rewardCard, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        String userName = oa_user.getUserName();
        int count = rewardsService.giveUserRewards(rewardCard, userName);
        if (count == 3) {
            return new APIResult(APIResult.SUCCESS, "领取礼包成功");
        }
        return new APIResult(APIResult.SUCCESS, "领取失败，请重试");
    }

    @DeleteMapping("delete")
    @RestPoint
    public APIResult deleteRewards(@RequestBody DeleteIds ids, HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
        if (oa_user.getUserRole() != 1) {
            log.warn(oa_user.getUserName() + " 尝试无权限修改");
            return new APIResult(APIResult.FAIL, "你莫得权限");
        }
        int count = rewardsService.deleteById(ids.getIds());
        if (count != 0) {
            log.warn(oa_user.getUserName() + " 删除了一份奖励");
            return new APIResult(APIResult.SUCCESS, "删除奖励成功");
        } else {
            return new APIResult(APIResult.FAIL, "删除失败，请重试");
        }
    }

}
